<template>
    <el-dialog v-model="localVisible" :title="props.title" :width="props.width + 'px'" :before-close="onClose"
        append-to-body :close-on-click-modal="false">
        <div class="container" :style="{ height: props.height + 'px' }">
            <slot name="content"></slot>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="danger" @click="onClose">取消</el-button>
                <el-button type="primary" @click="onConfirm">
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { DialogProps } from '@/type/dialog';
import { watch } from 'vue';
import { ref } from 'vue';


// interface DialogProps {
//     title: string,
//     visible: boolean,
//     width?: number,
//     height?: number
// }

const props = withDefaults(defineProps<DialogProps>(), {
    title: '标题',
    visible: false,
    width: 630,
    height: 280
});

// 接收父组件传递的方法
const emit = defineEmits(['close', 'confirm']);

// localVisible跟随props.visible的变化而变化
const localVisible = ref(props.visible);
watch(()=>props.visible, (newValue)=>{
    localVisible.value = newValue;
})


const onClose = () => {
    console.log('dialog closed');
    emit('close');
}

const onConfirm = () => {
    console.log('dialog confirm');
    emit('confirm');
}

</script>

<style lang="scss" scoped>
// 弹出框样式
.container {
    overflow-x: initial;
    overflow-y: auto;
}

.el-dialog {
    border-top-left-radius: 7px !important;
    border-top-right-radius: 7px !important;
    padding: 0;

    // 头部
    .el-dialog__header {
        margin-right: 0px;
        border-top-left-radius: 7px !important;
        border-top-right-radius: 7px !important;
        background-color: #304156 !important;
        padding: 10px;

        .el-dialog__title {
            color: #fff;
            font-size: 16px;
            font-weight: 600;
        }
    }

    .el-dialog__headerbtn {
        .el-dialog__close {
            color: #fff;
        }
    }

    // 主体内容
    .el-dialog__body {
        padding: 10px;
    }

    // 底部
    .el-dialog__footer {
        border-top: 1px solid #e8eaec !important;
        padding: 10px;
    }
}
</style>